package cn.anc.aonicardv.module.ui.recorder;

import android.app.Dialog;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Configs;
import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.AoniRecorderInfoUtils;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.util.LogUtil;
import cn.anc.aonicardv.util.StringUtils;
import cn.anc.aonicardv.widget.WarningDialog;
import cn.anc.httpcontrolutil.CarControl;

/**
 * Created by yangdai on 2017/6/22.
 */

public class WifiSettingActivity extends BaseActivity implements CarControl.OnHeartBeatCallBack, View.OnClickListener {

    @BindView(R2.id.tv_title)
    TextView titleTv;
    @BindView(R2.id.tv_wifi_ssd)
    EditText wifiSSDTv;
    @BindView(R2.id.edit_wifi_password_again)
    EditText wifiPasswordAgainEdit;
    @BindView(R2.id.ll_wifi_password_again)
    LinearLayout mLlWifiPasswordAgain;

    @BindView(R2.id.edit_wifi_password)
    EditText wifiPasswordEdit;
    @BindView(R2.id.tv_submit_ok)
    TextView submitTv;
    private DialogUtils dialogUtils;
    private Dialog progressDialog;
    private WarningDialog warningDialog;
    private WifiManager wifiManager;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_wifi_setting);
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initVariable() {
        dialogUtils = new DialogUtils();
        progressDialog = dialogUtils.getCustomProgressDialog(WifiSettingActivity.this);
        warningDialog = dialogUtils.getWarningDialog(WifiSettingActivity.this);
        wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

    }

    @Override
    public void setListener() {
        CarControl.setOnHeartBeatCallBack(this);
        warningDialog.okTv.setOnClickListener(this);
    }

    @Override
    public void initData() {
        wifiSSDTv.setFocusable(Configs.DEFUTL_EDIT_WIFI_NAME);
        titleTv.setText(getString(R.string.my_wifi_setting));
        wifiSSDTv.setText(MyApplication.wifiSSID);
        mLlWifiPasswordAgain.setVisibility(Configs.DEFUTL_EDIT_WIFI_PASSWORD_AGAIN ? View.VISIBLE : View.GONE);
    }


    @OnClick(R2.id.tv_back)
    public void back() {
        finish();
    }

    @OnClick(R2.id.tv_submit_ok)
    public void ok() {
        String wifiSsid = wifiSSDTv.getText().toString().trim().replace(" ", "");
        String passWord = wifiPasswordEdit.getText().toString().trim().replace(" ", "");
        String passWordAgain = wifiPasswordAgainEdit.getText().toString().trim().replace(" ", "");
        if (wifiSsid.length() < 4) {
            Toast.makeText(this, getString(R.string.my_wifi_password_setting_hint5), Toast.LENGTH_SHORT).show();
            return;
        }
        if (Configs.DEFUTL_EDIT_WIFI_PASSWORD_AGAIN) {
            if (!passWord.equals(passWordAgain)) {
                Toast.makeText(this, getString(R.string.my_wifi_password_setting_hint3), Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (passWord.length() < 8 || passWord.length() > 16) {
            Toast.makeText(this, getString(R.string.my_wifi_password_setting_hint3), Toast.LENGTH_SHORT).show();
            return;
        }
        if (!StringUtils.isContainNumberAndLetter(passWord)) {
            Toast.makeText(this, getString(R.string.my_wifi_password_setting_hint2), Toast.LENGTH_SHORT).show();
            return;
        }
        setWifiInfo();
    }

    private void setWifiInfo() {
        progressDialog.show();
        final String brand = CarControl.DeviceInfo.model;
        CarControl.doModifyWifiSetting(wifiSSDTv.getText().toString(), wifiPasswordEdit.getText().toString().trim().replace(" ", ""), new CarControl.OnCarControlCallback() {
            @Override
            public void OnCommandCallback(int code, CarControl.CommandResponseInfo info) {
                LogUtil.e("kkcTest", "---------------info:" + info);
                if (info.rval == 0) {
                    progressDialog.dismiss();
                    AoniRecorderInfoUtils.saveConnectedRecorderInfo(wifiSSDTv.getText().toString(), wifiPasswordEdit.getText().toString(), brand);
                    Toast.makeText(WifiSettingActivity.this, getString(R.string.my_wifi_password_setting_hint4), Toast.LENGTH_SHORT).show();
                } else {
                    progressDialog.dismiss();
                    Toast.makeText(WifiSettingActivity.this, getString(R.string.my_wifi_password_setting_hint6), Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        CarControl.setOnHeartBeatCallBack(this);
        if (!CarControl.IsConnected.get()){
            if (null != warningDialog) {
                if (warningDialog.isShowing()) {
                    warningDialog.dismiss();
                }
                if (!warningDialog.isShowing() && !isDestroyed()) {
                    warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
                }

            }
        }

    }
    @Override
    public void OnHeartBeatConnectOK() {
    }

    @Override
    public void OnHeartBeatConnectFail() {
    }

    @Override
    public void OnHeartBeatDisconnect() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (warningDialog != null)
                    warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
                wifiManager.setWifiEnabled(true);
            }
        }, 500);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok:
                warningDialog.dismiss();
                startActivity(new Intent(this, MainActivity.class));
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
